<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>账户挂失 - 银行客户系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            overflow-x: hidden;
        }

        .app-container {
            display: flex;
            min-height: 100vh;
            width: 100%;
        }

        .sidebar {
            width: 250px;
            background: linear-gradient(to bottom, #1a3a6c, #2c5a8d);
            color: #fff;
            padding: 30px 0;
            flex-shrink: 0;
        }

        .logo-container {
            padding: 0 25px 25px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .logo-icon {
            width: 50px;
            height: 50px;
            background-color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #1a3a6c;
            font-weight: bold;
        }

        .logo-text {
            font-size: 20px;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
            padding: 20px 25px;
            gap: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: #ffcc00;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 20px;
        }

        .user-details {
            flex: 1;
        }

        .user-name {
            font-weight: 600;
            margin-bottom: 5px;
            font-size: 16px;
        }

        .user-account {
            font-size: 13px;
            opacity: 0.8;
        }

        .sidebar-menu {
            padding: 0 15px;
        }

        .menu-item {
            padding: 14px 20px;
            display: flex;
            align-items: center;
            gap: 15px;
            cursor: pointer;
            transition: all 0.2s;
            border-radius: 8px;
            margin-bottom: 8px;
        }

        .menu-item:hover {
            background: rgba(255, 255, 255, 0.1);
        }

        .menu-item.active {
            background: rgba(255, 255, 255, 0.15);
            border-left: 4px solid #ffcc00;
        }

        .menu-item i {
            font-size: 18px;
            width: 24px;
            text-align: center;
        }

        .menu-text {
            font-size: 15px;
        }

        /* 右侧内容区样式 */
        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            overflow: auto;
        }

        .topbar {
            background-color: white;
            padding: 0 30px;
            height: 80px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-bottom: 1px solid #eee;
            flex-shrink: 0;
        }

        .page-title {
            font-size: 24px;
            font-weight: 600;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .page-title i {
            background-color: #1a3a6c;
            color: white;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
        }

        .current-time {
            font-size: 14px;
            color: #666;
            font-weight: 500;
        }

        .content-area {
            flex: 1;
            padding: 30px;
            overflow-y: auto;
            background-color: #f8fafc;
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        /* 挂失页面样式 */
        .lost-container {
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        .lost-info-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            padding: 30px;
        }

        .section-title {
            font-size: 20px;
            color: #1a3a6c;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #1a3a6c;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .section-title i {
            color: #1a3a6c;
        }

        .warning-box {
            background-color: #fef9c3;
            border: 1px solid #fde047;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 30px;
        }

        .warning-header {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 15px;
        }

        .warning-icon {
            color: #f59e0b;
            font-size: 24px;
        }

        .warning-title {
            font-size: 18px;
            font-weight: 600;
            color: #d97706;
        }

        .warning-content {
            line-height: 1.6;
            color: #854d0e;
        }

        .warning-content li {
            margin-bottom: 10px;
            margin-left: 20px;
        }

        .password-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px 0;
        }

        .password-title {
            font-size: 18px;
            color: #1a3a6c;
            margin-bottom: 20px;
            text-align: center;
        }

        /* 数字密码输入框样式 */
        .digit-inputs {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-bottom: 30px;
        }

        .digit-input {
            width: 50px;
            height: 60px;
            font-size: 28px;
            text-align: center;
            border: 2px solid #ddd;
            border-radius: 8px;
            background: white;
            outline: none;
            transition: all 0.2s;
        }

        .digit-input:focus {
            border-color: #1a3a6c;
            box-shadow: 0 0 5px rgba(26, 58, 108, 0.3);
        }

        .btn-lost {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background-color: #dc2626;
            color: white;
            padding: 15px 35px;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            border: none;
            transition: all 0.3s ease;
            margin-top: 15px;
        }

        .btn-lost:hover {
            background-color: #b91c1c;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(220, 38, 38, 0.3);
        }

        .result-container {
            margin-top: 30px;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            display: none;
        }

        .result-success {
            background-color: #dcfce7;
            border: 1px solid #22c55e;
            color: #166534;
        }

        .result-error {
            background-color: #fee2e2;
            border: 1px solid #ef4444;
            color: #b91c1c;
        }

        .result-icon {
            font-size: 40px;
            margin-bottom: 15px;
        }

        .result-title {
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .result-message {
            font-size: 16px;
            margin-bottom: 15px;
            line-height: 1.6;
        }

        /* 响应式设计 */
        @media (max-width: 900px) {
            .app-container {
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
            }
        }

        @media (max-width: 768px) {
            .digit-inputs {
                gap: 10px;
            }

            .digit-input {
                width: 40px;
                height: 50px;
                font-size: 24px;
            }
        }

        @media (max-width: 576px) {
            .topbar {
                padding: 0 20px;
                height: 70px;
                flex-direction: column;
                justify-content: center;
                gap: 5px;
            }

            .page-title {
                font-size: 20px;
            }

            .content-area {
                padding: 20px;
            }

            .lost-info-card {
                padding: 20px;
            }

            .btn-lost {
                padding: 12px 25px;
                font-size: 16px;
            }
        }

        /* 错误消息样式 */
        .error-message {
            color: #e74c3c;
            font-weight: 500;
            margin-top: 10px;
            text-align: center;
            display: none;
        }

        /* 倒计时样式 */
        .countdown-message {
            margin-top: 15px;
            font-weight: 500;
            font-size: 16px;
            color: #166534;
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- 左侧菜单 -->
        <div class="sidebar">
            <div class="logo-container">
                <div class="logo">
                    <div class="logo-icon">
                        <i class="fas fa-university"></i>
                    </div>
                    <div class="logo-text">银行系统</div>
                </div>
            </div>

            <div class="user-info">
                <div class="avatar" id="user-avatar"></div>
                <div class="user-details">
                    <div class="user-name" id="user-name"></div>
                    <div class="user-account" id="user-account"></div>
                </div>
            </div>

            <div class="sidebar-menu">
                <div class="menu-item" onclick="location.href='../user_home.html';">
                    <i class="fas fa-wallet"></i>
                    <div class="menu-text">账户信息</div>
                </div>
                <div class="menu-item" onclick="location.href='Deposit.html';">
                    <i class="fas fa-money-bill-wave"></i>
                    <div class="menu-text">存款</div>
                </div>
                <div class="menu-item" onclick="location.href='Withdraw money.html';">
                    <i class="fas fa-hand-holding-usd"></i>
                    <div class="menu-text">取款</div>
                </div>
                <div class="menu-item" onclick="location.href='Transfer.html';">
                    <i class="fas fa-exchange-alt"></i>
                    <div class="menu-text">转账</div>
                </div>
                <div class="menu-item active">
                    <i class="fas fa-lock"></i>
                    <div class="menu-text">账户挂失</div>
                </div>
                <div class="menu-item" onclick="location.href='ChangePassword.html';">
                    <i class="fas fa-key"></i>
                    <div class="menu-text">修改密码</div>
                </div>
                <div class="menu-item" onclick="location.href='TransactionRecords.html';">
                    <i class="fas fa-history"></i>
                    <div class="menu-text">交易记录</div>
                </div>
                <div class="menu-item" id="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <div class="menu-text">安全退出</div>
                </div>
            </div>
        </div>

        <!-- 右侧内容区 -->
        <div class="main-content">
            <div class="topbar">
                <div class="page-title">
                    <i class="fas fa-lock"></i>
                    <span>账户挂失</span>
                </div>
                <div class="current-time" id="current-time"></div>
            </div>

            <div class="content-area">
                <div class="lost-container">
                    <!-- 挂失信息卡片 -->
                    <div class="lost-info-card">
                        <h2 class="section-title">
                            <i class="fas fa-exclamation-triangle"></i>账户挂失操作
                        </h2>

                        <div class="warning-box">
                            <div class="warning-header">
                                <i class="fas fa-exclamation-circle warning-icon"></i>
                                <div class="warning-title">重要提示</div>
                            </div>
                            <div class="warning-content">
                                <p>账户挂失是一项重要安全操作，请在以下情况下执行：</p>
                                <ul>
                                    <li>您的银行卡丢失或被盗</li>
                                    <li>怀疑银行卡信息泄露</li>
                                    <li>发现未经授权的交易</li>
                                </ul>
                                <p>挂失后将立即冻结您的账户，所有交易将被暂停。如需恢复账户，请携带有效身份证件到柜台办理解挂手续。</p>
                            </div>
                        </div>

                        <div class="password-section">
                            <div class="password-title">
                                请输入您的6位银行卡密码以确认挂失操作
                            </div>

                            <div class="digit-inputs">
                                <input type="password" class="digit-input" maxlength="1" data-index="0">
                                <input type="password" class="digit-input" maxlength="1" data-index="1">
                                <input type="password" class="digit-input" maxlength="1" data-index="2">
                                <input type="password" class="digit-input" maxlength="1" data-index="3">
                                <input type="password" class="digit-input" maxlength="1" data-index="4">
                                <input type="password" class="digit-input" maxlength="1" data-index="5">
                            </div>

                            <button class="btn-lost" id="submit-lost">
                                <i class="fas fa-lock"></i> 确认挂失
                            </button>

                            <div class="error-message" id="error-message"></div>
                        </div>

                        <div class="result-container" id="result-container">
                            <!-- 结果内容将通过JS动态填充 -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 用户信息处理函数
        function renderUserInfo() {
            const cardID = sessionStorage.getItem('cardID');
            const userName = sessionStorage.getItem('userName');

            if (!cardID) {
                alert('请先登录系统');
                window.location.href = '../login.html';
                return;
            }

            const formattedCardID = cardID.replace(/(\d{4})(?=\d)/g, '$1 ');
            document.getElementById('user-account').textContent = `账号: ${formattedCardID}`;
            document.getElementById('user-name').textContent = userName;
            document.getElementById('user-avatar').textContent = userName.charAt(0);
        }

        // 更新时间显示
        function updateTime() {
            const now = new Date();
            const options = {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit',
                hour12: false
            };
            document.getElementById('current-time').textContent = now.toLocaleDateString('zh-CN', options);
        }

        // 密码输入框处理
        function setupPasswordInputs() {
            const inputs = document.querySelectorAll('.digit-input');

            inputs.forEach((input, index) => {
                // 输入处理
                input.addEventListener('input', function() {
                    if (this.value.length === 1 && index < inputs.length - 1) {
                        inputs[index + 1].focus();
                    }
                });

                // 退格键处理
                input.addEventListener('keydown', function(e) {
                    if (e.key === 'Backspace' && this.value === '' && index > 0) {
                        inputs[index - 1].focus();
                    }
                });
            });
        }

        // 获取输入的密码
        function getPassword() {
            const inputs = document.querySelectorAll('.digit-input');
            let password = '';

            inputs.forEach(input => {
                password += input.value;
            });

            return password;
        }

        // 重置密码输入框
        function resetPasswordInputs() {
            const inputs = document.querySelectorAll('.digit-input');
            inputs.forEach(input => {
                input.value = '';
            });
            inputs[0].focus();
        }

        // 显示结果
        function showResult(type, title, message) {
            const resultContainer = document.getElementById('result-container');
            let icon = '';
            let containerClass = '';

            if (type === 'success') {
                icon = '<i class="fas fa-check-circle result-icon"></i>';
                containerClass = 'result-success';
            } else {
                icon = '<i class="fas fa-times-circle result-icon"></i>';
                containerClass = 'result-error';
            }

            resultContainer.className = `result-container ${containerClass}`;
            resultContainer.innerHTML = `
                ${icon}
                <div class="result-title">${title}</div>
                <div class="result-message">${message}</div>
            `;
            resultContainer.style.display = 'block';
        }

        // 提交挂失请求
        async function submitLostRequest() {
            const cardID = sessionStorage.getItem('cardID');
            const password = getPassword();

            if (!cardID) {
                alert('请先登录系统');
                window.location.href = '../login.html';
                return;
            }

            // 验证密码长度
            if (password.length !== 6) {
                document.getElementById('error-message').textContent = '请输入6位密码';
                document.getElementById('error-message').style.display = 'block';
                return;
            }

            // 确认操作
            if (!confirm('确认挂失银行卡？挂失后账户将被冻结，无法进行任何交易！')) {
                return;
            }

            // 显示加载状态
            const button = document.getElementById('submit-lost');
            const errorMessage = document.getElementById('error-message');
            const originalHtml = button.innerHTML;
            button.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 处理中...';
            button.disabled = true;
            errorMessage.style.display = 'none';

            try {
                // 发送请求到后端API
                const response = await fetch('http://localhost:8080/customer/lost', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        cardID: cardID,
                        password: password
                    })
                });

                if (!response.ok) {
                    throw new Error(`服务器错误: ${response.status}`);
                }

                const result = await response.json();

                if (result.code === '200') {
                    // 挂失成功
                    showResult('success', '挂失成功', '您的账户已被冻结，请尽快到银行柜台办理后续手续。');

                    // 更新UI状态
                    button.innerHTML = '<i class="fas fa-check"></i> 挂失成功';
                    button.style.backgroundColor = '#22c55e';

                    // 禁用密码输入
                    document.querySelectorAll('.digit-input').forEach(input => {
                        input.disabled = true;
                    });

                    // 倒计时提示
                    const resultContainer = document.getElementById('result-container');
                    const countdownMessage = document.createElement('div');
                    countdownMessage.className = 'countdown-message';
                    resultContainer.appendChild(countdownMessage);

                    // 设置5秒倒计时
                    let seconds = 2;
                    countdownMessage.textContent = `系统将在${seconds}秒后自动跳转到登录页面...`;

                    const countdownInterval = setInterval(() => {
                        seconds--;
                        countdownMessage.textContent = `系统将在${seconds}秒后自动跳转到登录页面...`;

                        if (seconds <= 0) {
                            clearInterval(countdownInterval);
                            // 清除用户信息并跳转
                            sessionStorage.removeItem('cardID');
                            sessionStorage.removeItem('userName');
                            window.location.href = 'login.html';
                        }
                    }, 1000);
                } else {
                    throw new Error(result.message || '挂失操作失败');
                }
            } catch (error) {
                console.error('挂失操作出错:', error);
                button.innerHTML = originalHtml;
                button.disabled = false;

                // 显示错误信息
                errorMessage.textContent = `错误: ${error.message}`;
                errorMessage.style.display = 'block';

                // 设置红色警告样式
                button.style.backgroundColor = '#e74c3c';
                setTimeout(() => {
                    button.style.backgroundColor = '';
                }, 1500);

                // 显示错误结果
                showResult('error', '操作失败', error.message || '挂失操作失败，请稍后再试');
            } finally {
                // 重置密码输入框
                resetPasswordInputs();
            }
        }

        // 退出登录
        document.getElementById('logout-btn').addEventListener('click', function() {
            if(confirm('确定要退出系统吗？')) {
                sessionStorage.removeItem('cardID');
                sessionStorage.removeItem('userName');
                window.location.href = '../login.html';
            }
        });

        document.addEventListener('DOMContentLoaded', function() {
            renderUserInfo();
            updateTime();
            setupPasswordInputs();

            // 每秒更新一次时间
            setInterval(updateTime, 1000);

            // 挂失按钮事件
            document.getElementById('submit-lost').addEventListener('click', submitLostRequest);
        });
    </script>
</body>
</html>